// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   TableLister.java

package com.hz.supermanage.dao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import com.hz.framework.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import com.sybase.jdbc2.tds.SybTimestamp;

public class TableLister
{

	public TableLister()
	{
	}

	public String generateXML(Map emap)
	{
		StringBuffer sb = new StringBuffer(generateTableHeader(emap));
		sb.append(generateTHead(emap));
		sb.append("<tbody>\n");
		Collection listRows = (Collection) emap.get("Collection");
		Collection columns = (Collection) emap.get("Columns");
		Object val = null;
		label0:
		for (Iterator iterator = listRows.iterator(); iterator.hasNext(); sb.append("</n>\n"))
		{
			Map map = (Map)iterator.next();
			sb.append("<n>\n");
			Iterator itColumns = columns.iterator();
			do
			{
				if (!itColumns.hasNext())
					continue label0;
				Map column = (Map)itColumns.next();
				val = column.get("key");
				if (val != null )//|| (val instanceof Condition))
				{
					sb.append("<n><![CDATA[");
					sb.append(this.getJspString(map.get(column.get("key"))));
					sb.append("]]></n>");
				}
			} while (true);
		}

		sb.append("</tbody>\n");
		sb.append("</table>");
		return sb.toString();
	}

	public String generateTableHeader(Map emap)
	{
		int rows = Integer.parseInt((String)(emap.get("EXI.ROWS.COUNT")==null?"0":emap.get("EXI.ROWS.COUNT")));
		int currentPage = Integer.parseInt((String)(emap.get("EXI.PAGE.CURRENT")==null?"0":emap.get("EXI.PAGE.CURRENT")));
		int perPage = Integer.parseInt((String) (emap.get("EXI.PAGE.PER")==null?"0":emap.get("EXI.PAGE.PER")));
		int totalPage = Integer.parseInt((String) (emap.get("EXI.PAGE.TOTAL")==null?"0":emap.get("EXI.PAGE.TOTAL")));
		StringBuffer sb = new StringBuffer();
		sb.append("<table rows=\"");
		sb.append(rows);
		sb.append("\" current=\"");
		sb.append(currentPage);
		sb.append("\" perpage=\"");
		sb.append(perPage);
		sb.append("\" totalpage=\"");
		sb.append(totalPage);
		sb.append("\">");
		return sb.toString();
	}

	public String generateTHead(Map emap)
	{
		Collection columns = (Collection) emap.get("Columns");
		StringBuffer sb = new StringBuffer("<thead>\n");
		Object val = null;
		Iterator itColumns = columns.iterator();
		do
		{
			if (!itColumns.hasNext())
				break;
			Map column = (Map)itColumns.next();
			val = column.get("key");
			if (val != null )//|| (val instanceof Condition))
			{
				sb.append("<n>");
				sb.append(column.get("key"));
				sb.append("</n>");
			}
		} while (true);
		sb.append("</thead>\n");
		return sb.toString();
	}

	public String emptyList()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<table rows=\"0\" current=\"0\" perpage=\"0\" totalpage=\"0\"><tbody></tbody></table>");
		return sb.toString();
	}
	
	
	public static String getJspString(Object val)
	{
		if (val == null)
			return "";
		if (val instanceof String)
			if (StringUtils.isEmpty((String)val))
				return "";
			else
				return StringUtils.trim(String.valueOf(val));
		if (val instanceof SybTimestamp)
			return formatStringDate(val.toString());
		if (val instanceof Timestamp)
			return formatStringDate(val.toString());
		if (val instanceof BigDecimal)
			return val.toString();
		else
			return val.toString();
	}
	
	public static String formatStringDate(String str)
	{
		if (str != null)
		{
			String endStr = str.substring(11);
			if (endStr.equals("00:00:00.0"))
				return DateUtil.formatDbDateToWebDate(str);
			else
				return DateUtil.formatDbDateToWebDateTime(str);
		} else
		{
			return "";
		}
	}
}
